import type { LangConfig } from './code-types'
import { format } from 'sql-formatter'


function sortKeysDeep(obj: any): any {
    if (Array.isArray(obj)) {
        // 如果是数组，递归处理每一项
        return obj.map(sortKeysDeep)
    } else if (obj && typeof obj === 'object') {
        // 如果是对象，先排序key，再递归处理每个value
        return Object.keys(obj).reduce((acc, key) => {
            // 递归处理每个属性值，并赋值到新对象的对应key
            acc[key] = sortKeysDeep(obj[key])
            return acc
        },{} as Record<string, any>)
    }
    // 基本类型直接返回
    return obj
}


export const SUPPORTED_LANGS: Record<string, LangConfig> = {
    json: {
        name: 'JSON',
        formatter: (text) => {
            const obj = JSON.parse(text)
            return JSON.stringify(sortKeysDeep(obj), null, 2)
          },
        // @ts-ignore
        prism: () => import('prismjs/components/prism-json'),
    },

    javascript: {
        name: 'JavaScript',
        formatter: (text) =>
            text
                .replace(/([^;}])\n*$/g, '$1;')
                .replace(/{/g, ' {\n')
                .replace(/}/g, '\n}')
                .replace(/,/g, ',\n'),
        // @ts-ignore
        prism: () => import('prismjs/components/prism-javascript'),
    },

    typescript: {
        name: 'TypeScript',
        formatter: (text) => text.replace(/(:\s*)\b(?!any\b)(\w+)\b/g, '$1$2').replace(/</g, '\n<'),
        // @ts-ignore
        prism: () => import('prismjs/components/prism-typescript'),
    },

    python: {
        name: 'Python',
        formatter: (text) => text.replace(/\t/g, '    ').replace(/(\n)(?=\S)/g, '$1    '),
        // @ts-ignore
        prism: () => import('prismjs/components/prism-python'),
    },

    sql: {
        name: 'SQL',
        formatter: (text) =>
            format(text, {
                language: 'sql',
                keywordCase: 'upper',
            }),
        // @ts-ignore
        prism: () => import('prismjs/components/prism-sql'),
    },

    html: {
        name: 'HTML',
        formatter: (text) => text.replace(/></g, '>\n<').replace(/(<\/?[\w-]+)/g, '\n$1'),
        // @ts-ignore
        prism: () => import('prismjs/components/prism-xml-doc'),
    },

    css: {
        name: 'CSS',
        formatter: (text) =>
            text.replace(/{/g, ' {\n  ').replace(/}/g, '\n}\n').replace(/;/g, ';\n  '),
        // @ts-ignore
        prism: () => import('prismjs/components/prism-css'),
    },

    markdown: {
        name: 'Markdown',
        formatter: (text) => text.replace(/\n#{1,6}/g, '\n\n$&').replace(/(\S)\n(\S)/g, '$1\n\n$2'),
        // @ts-ignore
        prism: () => import('prismjs/components/prism-markdown'),
    },

    yaml: {
        name: 'YAML',
        formatter: (text) => text.replace(/(\w+):/g, '\n$1:').replace(/- /g, '  - '),
        // @ts-ignore
        prism: () => import('prismjs/components/prism-yaml'),
    },

    java: {
        name: 'Java',
        formatter: (text) => text.replace(/{/g, ' {\n').replace(/}/g, '\n}'),
        // @ts-ignore
        prism: () => import('prismjs/components/prism-java'),
    },

    csharp: {
        name: 'C#',
        formatter: (text) => text.replace(/{/g, ' {\n').replace(/}/g, '\n}'),
        // @ts-ignore
        prism: () => import('prismjs/components/prism-csharp'),
    },

    ruby: {
        name: 'Ruby',
        formatter: (text) => text.replace(/def\s+\w+/g, '\n$&').replace(/end/g, '\nend\n'),
        // @ts-ignore
        prism: () => import('prismjs/components/prism-ruby'),
    },

    rust: {
        name: 'Rust',
        formatter: (text) => text.replace(/{/g, ' {\n').replace(/}/g, '\n}'),
        // @ts-ignore
        prism: () => import('prismjs/components/prism-rust'),
    },

    go: {
        name: 'Go',
        formatter: (text) => text.replace(/func\s+\w+/g, '\n$&').replace(/}/g, '\n}'),
        // @ts-ignore
        prism: () => import('prismjs/components/prism-go'),
    },
}
